//
//  BlogCell.m
//  QSBK
//
//  Created by Eric Wang on 4/20/15.
//  Copyright (c) 2015 Eric Wang. All rights reserved.
//

#import "BlogCell.h"
#import "UIImageView+WebCache.h"

@implementation BlogCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self addAllSubviews];
    }
    return self;
}


-(void)addAllSubviews
{
    self.contentView.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0];
//    @property(nonatomic,strong)UIView *backGroundView;
    self.bgView = [[UIView alloc]init];
    [self.bgView.layer setCornerRadius:5.0];
    self.bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.bgView];
//    //touxiang
//    @property(nonatomic,strong)UIImageView *headImagee;
    self.headImage = [[UIImageView alloc]init];
    [self.headImage.layer setCornerRadius:20.0];
    self.headImage.layer.masksToBounds = YES;
    
    [self.bgView addSubview:self.headImage];
//    //name
//    @property(nonatomic,strong)UILabel *nameLabel;
    self.nameLabel = [[UILabel alloc]init];
    [self.bgView addSubview:self.nameLabel];
//    //contentImage
//    @property(nonatomic,strong)UIImageView *contentImage;
    self.contentImage = [[UIImageView alloc]init];
    [self.bgView addSubview:self.contentImage];
//    //contentLabel
//    @property(nonatomic,strong)UILabel *contentLabel;
    self.contentLabel = [[UILabel alloc]init];
    UIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:15];
    self.contentLabel.font = fnt;
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.lineBreakMode = NSLineBreakByWordWrapping;
    [self.bgView addSubview:self.contentLabel];
//    //vedio
//    @property(nonatomic,strong)UIImageView *video;
    self.picImageView = [[UIImageView alloc]init];
    
    self.playButton = [[UIButton alloc]init];
    self.picImageView.userInteractionEnabled = YES;
    [self.playButton setImage:[UIImage imageNamed:@"video_play_normal.png"] forState:UIControlStateNormal];
    [self.playButton addTarget:self action:@selector(videoPlay:) forControlEvents:UIControlEventTouchUpInside];
    [self.picImageView addSubview:self.playButton];
    [self.bgView addSubview:self.picImageView];
//    //upLabel
//    @property(nonatomic,strong)UILabel *upLabel;
    self.upLabel = [[UILabel alloc]init];
    [self.bgView addSubview:self.upLabel];
//    //commentLabel
//    @property(nonatomic,strong)UILabel *commentLabel;
    self.commentLabel = [[UILabel alloc]init];
    [self.bgView addSubview:self.commentLabel];
//    //up
//    @property(nonatomic,strong)UIButton *upButton;
    self.upButton = [[UIButton alloc]init];
    self.upButton.tag = 1111;
    [self.upButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.upButton setImage:[UIImage imageNamed:@"operation_support.png" ] forState:UIControlStateNormal];
    [self.upButton setImage:[UIImage imageNamed:@"operation_support_press.png"] forState:UIControlStateSelected];
    [self.bgView addSubview:self.upButton];
//    //down
//    @property(nonatomic,strong)UIButton *downButton;
    self.downButton = [[UIButton alloc]init];
    self.downButton.tag = 1112;
    [self.downButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.downButton setImage:[UIImage imageNamed:@"operation_unsupport.png"] forState:UIControlStateNormal];
    [self.downButton setImage:[UIImage imageNamed:@"operation_unsupport_press.png"] forState:UIControlStateSelected];
    [self.bgView addSubview:self.downButton];
//    //commentButton
//    @property(nonatomic,strong)UIButton *commentButton;
    self.commentButton = [[UIButton alloc]init];
    self.commentButton.tag = 1113;
    [self.commentButton addTarget:self  action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.commentButton setImage:[UIImage imageNamed:@"operation_comments.png"] forState:UIControlStateNormal];
    [self.bgView addSubview:self.commentButton];
//    //more
//    @property(nonatomic,strong)UIButton *moreButton;
    self.moreButton = [[UIButton alloc]init];
    self.moreButton.tag = 1114;
    [self.moreButton addTarget:self action:@selector(messageAndMoreButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.moreButton setImage:[UIImage imageNamed:@"operation_more_night.png"] forState:UIControlStateNormal];
    
    [self.bgView addSubview:self.moreButton];
}

-(void)setBlogFrame:(BlogFrame *)blogFrame
{
    _blogFrame = blogFrame;
    
    self.bgView.frame = _blogFrame.bgView;
    
    self.headImage.frame = _blogFrame.headImage;
    if(_blogFrame.blog.user.icon==nil)
    {
        self.headImage.image = [UIImage imageNamed:@"default_anonymous_users_avatar.png"];
    }else{
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:_blogFrame.blog.user.icon] placeholderImage:[UIImage imageNamed:@"default_users_avatar.png"]];
    }
    
    
    if(_blogFrame.blog.user.user_id == nil)
    {
        self.headImage.image = [UIImage imageNamed:@"default_anonymous_users_avatar.png"];
        self.nameLabel.textColor = [UIColor colorWithRed:170/255.0 green:170/255.0 blue:170/255.0 alpha:1.0];
    }else
    {
        self.nameLabel.textColor = [UIColor blackColor];
    }
    self.nameLabel.frame = _blogFrame.nameLabel;
    self.nameLabel.text = _blogFrame.blog.user.login;
    
    self.contentLabel.frame = _blogFrame.contentLabel;
    self.contentLabel.text = _blogFrame.blog.content;
    
    
    self.contentImage.frame = _blogFrame.contentImage;
    [self.contentImage sd_setImageWithURL:[NSURL URLWithString:_blogFrame.blog.image] placeholderImage:[UIImage imageNamed:@"bg_light_tile.png"]];

    
    
    self.picImageView.frame = _blogFrame.picView;
    
    self.playButton.frame = _blogFrame.playButton;
    [self.picImageView sd_setImageWithURL:[NSURL URLWithString:_blogFrame.blog.pic_url] placeholderImage:[UIImage imageNamed:@"bg_light_title.png"]];
    
    self.upLabel.frame = _blogFrame.upLabel;
    [self.upLabel setTextColor:[UIColor colorWithRed:120/255.0 green:120/255.0 blue:120/255.0 alpha:1.0]];
    self.upLabel.text = [NSString stringWithFormat:@"好笑%d",[_blogFrame.blog.up intValue]];
    
    self.commentLabel.frame = _blogFrame.commentLabel;
    [self.commentLabel setTextColor:[UIColor colorWithRed:120/255.0 green:120/255.0 blue:120/255.0 alpha:1.0]];
    self.commentLabel.text = [NSString stringWithFormat:@"评论%d",[_blogFrame.blog.comments_count intValue]];
    
    self.upButton.frame = _blogFrame.upButton;
    
    
    self.downButton.frame = _blogFrame.downButton;
    
    
    
    self.commentButton.frame = _blogFrame.commentButton;
    
    
    self.moreButton.frame = _blogFrame.moreButton;
}



// up and down button click
-(void)buttonClick:(UIButton *)button
{
    
    button.selected = YES;
    self.selectedButton.selected = NO;
    
    self.selectedButton = button;
    switch (button.tag) {
        case 1111:
            if(self.delegate!=nil&&[self.delegate respondsToSelector:@selector(ClickUpWithCell:)])
            {
                [self.delegate ClickUpWithCell:self];
                _blogFrame.blog.up = @([_blogFrame.blog.up intValue]+1);
                self.upLabel.text = [NSString stringWithFormat:@"好笑%d",[_blogFrame.blog.up intValue]];
            }
            break;
        case 1112:
            if(self.delegate!=nil&&[self.delegate respondsToSelector:@selector(ClickUpWithCell:)])
            {
                [self.delegate ClickDownWithCell:self];
                _blogFrame.blog.up = @([_blogFrame.blog.up intValue]-1);
                self.upLabel.text = [NSString stringWithFormat:@"好笑%d",[_blogFrame.blog.up intValue]];
            }
            break;
        case 1113:
            if(self.delegate!=nil&&[self.delegate respondsToSelector:@selector(ClickUpWithCell:)])
            {
                [self.delegate ClickCommentWithCell:self];
            }
            break;
            
        default:
            break;
    }
}

// messgage and more
-(void)messageAndMoreButtonClick:(UIButton *)button
{
    if(self.delegate!=nil&&[self.delegate respondsToSelector:@selector(ClickMoreWithCell:)])
    {
        [self.delegate ClickMoreWithCell:self];
    }
}


-(void)videoPlay:(UIButton *)button
{
    
    HomeVedioPlayerController *videoPlayer  = [[HomeVedioPlayerController alloc] initWithContentURL:[NSURL URLWithString:_blogFrame.blog.low_url]];
    self.playButton.hidden = YES;
    videoPlayer.view.frame = self.picImageView.bounds;
    ;
    videoPlayer.delegate = self;
    videoPlayer.imageData = _picImageView.image;
    videoPlayer.low_url = [NSURL URLWithString:_blogFrame.blog.low_url];
    videoPlayer.high_url = [NSURL URLWithString:_blogFrame.blog.high_url];
    [self.picImageView addSubview:videoPlayer.view];
}

-(void)movieDidStop
{
    self.playButton.hidden = NO;
}
@end
